package ca.cbc.msb;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.Session;
import javax.naming.InitialContext;
import javax.naming.NamingException;


/**
 * @author : Arash Bizhanzadeh <arash.bizhanzadeh@cbc.ca>
 *         Date: 11-09-22
 *         Time: 1:08 PM
 *         CBC.ca All rights reserved.
 */
public class DeadLetterQ {

    public static void main(String[] args){
            Connection connection = null;
             InitialContext initialContext = null;

             try
             {
                // Step 1. Create an initial context to perform the JNDI lookup.
                initialContext = new InitialContext();
                // Step 3. Perform a lookup on the Connection Factory
                ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");

                // Step 4.Create a JMS Connection
                connection = cf.createConnection();

                // Step 5. Create a * transacted* JMS Session
                Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

                connection.start();


                // We will now consume the message from the dead letter queue

                // Step 17. Perform a lookup on the dead letter queue
                Queue deadLetterQueue = (Queue)initialContext.lookup("/queue/DLQ");

                // Step 18. Create a JMS Message Consumer for the dead letter queue
                MessageConsumer deadLetterConsumer = session.createConsumer(deadLetterQueue);
                deadLetterConsumer.setMessageListener( new MessageListener() {
                    int i = 0;
                    @Override
                    public void onMessage(Message message) {
                        System.out.println("message received " + i++);
                        System.out.println(message.toString());
                    }
                });
                new Thread(session).start();
                 while(true){
                     Thread.sleep(5000);
                 }
//                 for(int i =0;i<5;i++){
//
//                // Step 19. Receive the message from the dead letter queue
//                Message messageReceived = deadLetterConsumer.receive();
//                if (messageReceived ==null){
//                    System.out.println("Dead letters finished");
//                    break;
//                }
//                // Step 20. The message sent to the queue was moved to the dead letter queue after 3 unsuccessful deliveries
//                System.out.println("Received message from " + deadLetterQueue.getQueueName() +
//                                   ": " +
//                                   messageReceived.toString());
//
//                // The message received from the dead letter queue has the same content than the undelivered message but its
//                // JMS headers
//                // differ (from JMS point of view, it's not the same message).
//                // HornetQ defines additional properties for messages received from the dead letter queue
//
//                System.out.println();
//                // Step 23. This time, we commit the session, the delivery from the dead letter queue is successful!
//                session.commit();
//                 }


    } catch (JMSException e) {
                 e.printStackTrace();
             } catch (NamingException e) {
                 e.printStackTrace();
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
    }
}
